package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 查询是否有一个分类被套餐所关联
     *
     * @param id 分类的唯一标识
     * @return 当根据id查询到有数据关联返回false
     */
    @Override
    public boolean hasCateId(Long id) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", id);
        if (setmealMapper.selectList(queryWrapper).size() == 0) {
            return false;
        }
        return true;
    }

    @Override
    public void saveSetmealAndSetmealDish(HttpServletRequest request, SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        setmeal.setImage((String) request.getSession().getAttribute("image"));
        setmealMapper.insert(setmeal);
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : list) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public R pageDish(int page, int pageSize, String name) {
        //构造分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> pageInfoNew = new Page<>(page, pageSize);
        //构造条件构造器
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        if (name != null) {
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByDesc("update_time");
        setmealMapper.selectPage(pageInfo, queryWrapper);
        BeanUtils.copyProperties(pageInfo, pageInfoNew, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(item, dto);
            Long categoryId = item.getCategoryId();
            Category category = categoryMapper.selectById(categoryId);
            String name1 = category.getName();
            dto.setCategoryName(name1);
            return dto;
        }).collect(Collectors.toList());
        //排序条件
        // 执行查询
        pageInfoNew.setRecords(list);
        return R.success(pageInfoNew);
    }

    @DeleteMapping
    public R deleteDish(Long[] ids) {
        for (Long id : ids) {
            setmealMapper.deleteById(id);
        }
        //TODO完善删除逻辑确保另一个表里的数据也被删除了。
        return R.success("删除成功");
    }

    @Override
    public SetmealDto getSetmealById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id", setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    @Override
    public void updateSetmeal(HttpServletRequest request, SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        BeanUtils.copyProperties(setmealDto, setmeal);
        setmeal.setImage((String) request.getSession().getAttribute("image"));
        setmealMapper.updateById(setmeal);
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < setmealDishes.size(); i++) {
            list.add(setmealDishes.get(i).getId());
        }
        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id", setmeal.getId());
        List<SetmealDish> dishes = setmealDishMapper.selectList(queryWrapper);
        for (int i = 0; i < dishes.size(); i++) {
            if (!list.contains(dishes.get(i).getId())) {
                setmealDishMapper.deleteById(dishes.get(i).getId());
            }
        }

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            if (setmealDish.getId() == null) {
                setmealDishMapper.insert(setmealDish);
            }
            setmealDishMapper.updateById(setmealDish);
        }
    }

    @Override
    public void updateStatusById(Integer status, Long[] ids) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        for (Long id :
                ids) {
            setmeal.setId(id);
            setmealMapper.updateById(setmeal);
        }
    }

    @Override
    public List<Setmeal> selectListByCaGorId(Long categoryId, Integer status) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId).eq("status",status);
        return setmealMapper.selectList(queryWrapper);
    }

}
